package com.speechb.calc;

import org.junit.Test;

public class BinarySearch {
    @Test
    public void test() {
        System.out.println(guessNumber(2126753390));;
    }

    public int guessNumber(int n) {
        int start = 0;
        int end = n;
        while(true) {
            int mid = (int) ((start + (long)end) / 2);
            int result = guess(mid);
            if (result == 0) {
                return mid;
            } else if (result == -1) {
                end = mid - 1;
            } else {
                start = mid + 1;
            }
        }
    }

    private int binarySearch(int start, int end) {
        int mid = (start + end) / 2;
        int result = guess(mid);
        if (result == 0) {
            return mid;
        } else if (result == -1) {
            end = mid - 1;
        } else {
            start = mid + 1;
        }
        return binarySearch(start, end);
    }


    int pick = 1702766719;
    private int guess(int mid) {
        if (mid == pick) {
            return 0;
        } else if (pick < mid) {
            return -1;
        } else {
            return 1;
        }
    }
}
